using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;

namespace Elementum
{
    public class Floor : MechanismGound
    {
        Player _player;
        ModifierContain _motionContain;
        PositionModifier _up;
        PositionModifier _down;
        protected Animation _buttonSprite;
        Animation _climb;

        public Floor(World world, Stage stage)
            : base(world, stage)
        {
            _buttonSprite = new Animation("images/system/button", new Rectangle(104, 308, 100, 100), new Point(100, 100));
            _climb = new Animation("images/player/player", new Rectangle(384, 0, 96, 60), new Point(48, 60));
            _climb.DeltaTime = 80;
        }

        

        protected override void SetSize(Vector2 size)
        {
            base.SetSize(size);
            _fixture.SetSensor(true);
        }



        private void Motion(VisibleObject owner, TouchLocationState state)
        {
            if (state == TouchLocationState.Pressed && _player.ModifierContainer == null)
            {
                //GameStage.ActionButtom.ButtonSprite = _buttonSprite;
                GameStage.LockTouch = true;
                //_player._fixture.SetSensor(true);
                _player.GetBody().SetType(BodyType.Static);
                if (_player.Position.Y < Position.Y)
                {//go down
                    _motionContain = new ModifierContain(_player);
                    _down = new PositionModifier(_player.Position, Position + new Vector2(0, Size.Y / 2), 10, (int)(10 + Size.Y * 0.6 / 2));
                    _down.ModifyDoneAction += ModifierDone;
                    _down.OnModifyAction += OnMove;
                    _motionContain.AddModifier(_down);
                    _player.ModifierContainer = _motionContain;
                }
                else
                {//go up
                    _motionContain = new ModifierContain(_player);
                    _up = new PositionModifier(_player.Position, Position - new Vector2(0, Size.Y / 2 + 20), 10, (int)(10 + Size.Y * 0.6 / 2));
                    _up.ModifyDoneAction += ModifierDone;
                    _up.OnModifyAction += OnMove;
                    _motionContain.AddModifier(_up);
                    _player.ModifierContainer = _motionContain;
                }
            }
        }

        public void OnMove() {
            GameStage.ActionButtom.ButtonSprite = _buttonSprite;
            _player.CurrentAnimation = _climb;
        }

        public void ModifierDone()
        {
            _player.ModifierContainer = null;
            _player._fixture.SetSensor(false);
            GameStage.LockTouch = false;
            _player.GetBody().SetType(BodyType.Dynamic);
        }

        public override void BeginContactAction(Contact contact, Body contactBody, float normal)
        {
            base.BeginContactAction(contact, contactBody, normal);
            if (contactBody.GetUserData() == "player")
            {
                _player = (Player)contactBody.Owner;
                _player.ActionEvent += Motion;
                GameStage.ActionButtom.Enable = true;
                GameStage.ActionButtom.ButtonSprite = _buttonSprite;
            }
        }

        public override void EndContactAction(Contact contact, Body contactBody, float normal)
        {
            base.EndContactAction(contact, contactBody, normal);
            if (contactBody.GetUserData() == "player")
            {
                _player.ActionEvent -= Motion;
                if (!GameStage.LockTouch)
                {
                    GameStage.ActionButtom.Enable = false;
                    GameStage.ActionButtom.ButtonSprite = null;
                }   
            }
        }

    }
}
